package com.yz.jdd.module.main;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yz.jdd.R;
import com.yz.jdd.base.MyApplication;
import com.yz.jdd.base.activity.BaseActivity;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.Constants;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.PreferencesUtils;
import com.yz.jdd.module.main.adapter.ShopsAdapter;
import com.yz.jdd.module.main.entity.ShopListItem;
import com.yz.jdd.module.main.presenter.ShopListContract;
import com.yz.jdd.module.main.presenter.ShopListPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 订单门店筛选 / 语音播报设置门店 / 电子券核销门店
 * 19/01/26 jmliu
 */


public class ChooseShopActivity extends BaseActivity implements ShopListContract.View {


    @BindView(R.id.top_back)
    TextView topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.search_et)
    EditText searchEt;
    @BindView(R.id.select_img)
    ImageView selectImg;
    @BindView(R.id.allshop_tv)
    TextView allshopTv;
    @BindView(R.id.allshop_layout)
    RelativeLayout allshopLayout;
    @BindView(R.id.expand_list)
    ExpandableListView expand_list;


    private String shopIdPar;
    private String shopNameStr;
    private String shopPayList;
    private ShopListContract.Presenter presenter;
    private ShopsAdapter shopExtendableListViewAdapter;

    boolean isSettingsActivity = false;
    String from;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_shop);
        ButterKnife.bind(this);


        shopIdPar = getIntent().getStringExtra("shopId");
        shopPayList  = getIntent().getStringExtra("shopPayList");
        isSettingsActivity = getIntent().getBooleanExtra("isSettingsActivity", false);
        //from不为空表示从扫码核销页面
        from=getIntent().getStringExtra("from");
//        if (!StringUtil.isBlank(shopIdPar)) {
//            selectImg.setVisibility(View.GONE);
//        }
        if (isSettingsActivity) {
            topTitle.setText("选择门店");
            allshopLayout.setVisibility(View.GONE);
        } else {
            topTitle.setText("筛选");
        }
        presenter = new ShopListPresenter(ChooseShopActivity.this, R.layout.item_order_list, this,isSettingsActivity,from);
        if(!TextUtils.isEmpty(shopPayList)){
            presenter.doGetPayShopList(shopIdPar);
        }else {
            if(!TextUtils.isEmpty(from)){
                presenter.doGetEvoucherShopList(shopIdPar);
            }else{
                presenter.doGetShopList(shopIdPar);
            }

        }


        searchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                LogUtils.i("beforeTextChanged---" + charSequence.toString());
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                LogUtils.i("onTextChanged---" + charSequence.toString());
                presenter.doSearch(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    @Override
    public void setPresenter(ShopListPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void expandGroup(int size) {
        int groupCount = expand_list.getCount();
        for (int i = 0; i < size; i++) {
            expand_list.expandGroup(i);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            resultFilterAct();
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.top_back, R.id.allshop_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_back:
                if (!isSettingsActivity) {
                    resultFilterAct();
                }else{
                    finish();
                }
                break;
            case R.id.allshop_layout:
                if (selectImg.getVisibility() == View.GONE) {
                    selectImg.setVisibility(View.VISIBLE);
                    allshopTv.setTextColor(Color.parseColor("#459dff"));
                    shopIdPar = "";
                }
                presenter.chooseAllShop();
                break;
        }
    }


    @Override
    public void selectedShop_SetActivity(ShopListItem.ResultObjectBean.PageDataBean.ShopsBean shopsBean) {
        Bundle bundle = new Bundle();
        Intent intent = new Intent();
        intent.putExtra("shopsBean", shopsBean);
        intent.putExtras(bundle);
        ChooseShopActivity.this.setResult(Constants.SettingsActivityForResultCode, intent);
        finish();
    }

    @Override
    public void selectedShop_ScanEvoucherActivity(ShopListItem.ResultObjectBean.PageDataBean.ShopsBean shopsBean) {
       // 这个地方把 默认核销门店写进去即可
//        Bundle bundle = new Bundle();
//        Intent intent = new Intent();
//        intent.putExtra("shopsBean", shopsBean);
//        intent.putExtras(bundle);
//        ChooseShopActivity.this.setResult(Constants.SettingsActivityForResultCode, intent);
        PreferencesUtils.putString(MyApplication.application, BaseConfigs.PREF_EVOUCHER_SHOPID, shopsBean.getShopId());
        PreferencesUtils.putString(MyApplication.application, BaseConfigs.PREF_EVOUCHER_SHOPNAME, shopsBean.getShopName());
        finish();

    }

    @Override
    public void selectedAllShop() {
        resultFilterAct();
    }

    @Override
    public void selectedOtherShop(String id, String name, boolean isFinish) {
        shopIdPar = id;
        shopNameStr = name;
        selectImg.setVisibility(View.GONE);
        allshopTv.setTextColor(Color.parseColor("#000000"));
        if (isFinish) {
            resultFilterAct();
        }
    }

    @Override
    public void setAdapter() {
        shopExtendableListViewAdapter = presenter.getAdapter();
//        orderListAdapter.addHeaderView(getHeaderView());
        expand_list.setAdapter(shopExtendableListViewAdapter);


        //设置分组的监听
        expand_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                //  Toast.makeText(getApplicationContext(), groupString[groupPosition], Toast.LENGTH_SHORT).show();
                return true; // true为不能点击收缩：
            }
        });
        //设置子项布局监听
        expand_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                //  Toast.makeText(getApplicationContext(), childString[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
                if (isSettingsActivity) {
                    // 设置语音 , 选择门店
                    presenter.chooseShop_SetActivity(groupPosition, childPosition);
                } else {

                    if(!TextUtils.isEmpty(from)){
                        //来自扫码页面
                        presenter.chooseEvoucherShop(groupPosition, childPosition);

                    }else{
                        presenter.chooseOtherShop(groupPosition, childPosition);
                    }

                }


                return true;

            }
        });
    }

    private void resultFilterAct() {
        Bundle bundle = new Bundle();
        Intent intent = new Intent();
        intent.putExtra("shopId", shopIdPar);
        intent.putExtra("shopName", shopNameStr);
        intent.putExtras(bundle);
        ChooseShopActivity.this.setResult(Constants.FilterActivityForResultCode, intent);
        finish();
    }

}
